home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Snippets / Devices / PBxxx Serial Demo / SerDemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-13  |  18.5 KB  |  685 lines  |  [TEXT/MPS ]

  1.  
  2. #include    <Types.h>
  3. #include    <SysEqu.h>
  4. #include    <Resources.h>
  5. #include    <QuickDraw.h>
  6. #include    <Fonts.h>
  7. #include    <Events.h>
  8. #include    <Menus.h>
  9. #include    <Controls.h>
  10. #include    <Dialogs.h>
  11. #include    <Memory.h>
  12. #include    <Files.h>
  13. #include    <Devices.h>
  14. #include    <Serial.h>
  15. #include    <Timer.h>
  16. #include    <OSUtils.h>
  17. #include    <GestaltEqu.h>
  18. #include    <Errors.h>
  19. #include    <string.h>
  20.  
  21.  
  22.  
  23. #define        rMonitor        1000
  24. #define        kSendButton        1
  25. #define        kStopButton        2
  26. #define        kQuitButton        3
  27. #define        kMsgBox            4
  28. #define        kSendSpinner    5
  29. #define        kRcvSpinner        7
  30. #define        kHoldFlag        9
  31. #define        kBreakButton    10
  32.  
  33. #define        rSerDataRsrc    128
  34. #define        rSpinnerIcon    1000
  35. #define        rHeldIcon        128
  36. #define        rNotHeldIcon    129
  37.  
  38. #define        rPortOpenALRT    256
  39. #define        kReset            2
  40.  
  41. #define        kCtlEnable        0
  42. #define        kCtlDisable        255
  43. #define        kSerBufSize        16384
  44. #define        kSerRdSize        64
  45. #define        kSerConfig        baud9600 + noParity + data8 + stop10
  46. #define        kBreakLength    666                /* serial break length in milliseconds */
  47.  
  48. enum        {drvrName = 0x12};                /* offset to driver name in 'DRVR' std. header */
  49. enum        {dOpened = 5,
  50.                 dRAMBased = 6,
  51.                 drvrActive = 7};            /* Device Manager DCtlFlag bits */
  52.                 
  53. enum        {kSerStatus = 8,
  54.                 kSerClrBrk = 11,            /* Serial Driver csCodes */
  55.                 kSerSetBrk = 12,
  56.                 kSerHShakeDTR = 14};
  57. enum        {breakR0 = 128};                /* mask for break bit in SCC RR0 -- See TN #56 */
  58. enum        {breakErr = 8};                    /* mask for break bit in cumErrs -- System 7.0 */
  59.  
  60.  
  61.  
  62. typedef struct {
  63.     IOParam        fIOParam;
  64.     long        appA5;
  65. } ExtIOParam;
  66.  
  67. typedef struct {
  68.     TMTask        fTMTask;
  69.     long        appA5;
  70. } ExtTMTask;
  71.  
  72. typedef struct {
  73.     unsigned char    readR0;
  74.     unsigned char    deltaBits;
  75.     short            drvrPosting;
  76. } SERDEventMessage;
  77.  
  78.  
  79.  
  80. Boolean        gHeldOff,
  81.             gAllDone = false,
  82.             gShouldSend = false,
  83.             gReload = false,
  84.             gKillBreak = false,
  85.             gBreakReceived = false;
  86. Ptr            gpSerBuf, *ghSerBuf,
  87.             gpOutputData, *ghOutputData,
  88.             gpBitBucket, *ghBitBucket;
  89. long        gOutputDataSize;
  90. IOParam        *gpSendPB, **ghSendPB;
  91. short        gSysVersion,
  92.             gOutRefNum, gInRefNum,
  93.             gSendCount = 0,
  94.             gRcvCount = 0;
  95. char        *panicString = "Help! I'm stuffed! And here's a bunch of characters to prove it!\n\r";
  96. ExtTMTask    gUnBreakTask;
  97.  
  98.  
  99.  
  100. short    Initialize (void);
  101. void    CleanUp (void);
  102. Boolean    OpenSERD (void);
  103. void    CloseSERD (void);
  104. void    DoIOStuff (void);
  105. OSErr    PrimeSend (void);
  106. void    CheckSerStatus (void);
  107. void    CheckSerData (long reqBytes);
  108. pascal Boolean NullGrabber (DialogPtr, EventRecord *evt, short *itemHit);
  109. pascal void    SendCompRout (void);
  110. pascal void FlagBreakTimeout (void);
  111. OSErr    AssertDrvrOpen (Str255 name, short *refNum);
  112.  
  113. ParmBlkPtr GetParmBlkPtr (void) = 0x2008;        /* MOVE.L A0,D0 */
  114. TMTaskPtr GetTMTaskPtr (void) = 0x2009;            /* MOVE.L A1,D0 */
  115.  
  116. #pragma parameter __D0 PBControlImmed(__A0)
  117. pascal OSErr PBControlImmed(ParmBlkPtr paramBlock) = 0xA204;        /* _Control ,IMMED */
  118.  
  119. #pragma parameter __D0 PBStatusImmed(__A0)
  120. pascal OSErr PBStatusImmed(ParmBlkPtr paramBlock) = 0xA205;            /* _Status ,IMMED */
  121.  
  122.  
  123.  
  124. void main (void)
  125. {
  126.     InitGraf(&qd.thePort);
  127.     InitFonts();
  128.     InitWindows();
  129.     InitMenus();
  130.     InitDialogs(NULL);
  131.     InitCursor();
  132.     
  133.     if (Initialize() == noErr) {
  134.         
  135.         if (OpenSERD()) {    /* open the Serial Driver */
  136.             DoIOStuff();
  137.             CloseSERD();    /* close the Serial Driver */
  138.         }
  139.         CleanUp();
  140.     }
  141.     
  142. }
  143.  
  144.  
  145.  
  146. Boolean OpenSERD (void)
  147. {
  148.     OSErr    openOutErr, openInErr;
  149.     OSErr    setBufErr, setCfgErr, setHskErr;
  150.     SerShk    hskFlags;
  151.     long    finalTicks;
  152.     Boolean    takeOverPort = true;
  153.     Boolean    openAOut, openAIn;
  154.     
  155.     openAOut = AssertDrvrOpen("\p.AOut", &gOutRefNum) == noErr;
  156.     openAIn = AssertDrvrOpen("\p.AIn", &gInRefNum) == noErr;
  157.     if (openAOut || openAIn) {
  158.         if (takeOverPort = CautionAlert(rPortOpenALRT, nil) == kReset) {
  159.             if (openAIn) {
  160.                 KillIO(gInRefNum);
  161.                 CloseDriver(gInRefNum);
  162.             }
  163.             if (openAOut) {
  164.                 KillIO(gOutRefNum);
  165.                 CloseDriver(gOutRefNum);
  166.             }
  167.         }
  168.     }
  169.     
  170.     if (takeOverPort) {
  171.         openOutErr = OpenDriver("\p.AOut", &gOutRefNum);
  172.         openInErr = OpenDriver("\p.AIn", &gInRefNum);
  173.         
  174.         if (openOutErr == noErr && openInErr == noErr) {
  175.         
  176.             /* There is a bug in the Macintosh IIfx IOP serial driver, in which      */
  177.             /* SerGetBuf() will always return zero characters, even if characters    */
  178.             /* have been received. The bug is exhibited when a remote serial device  */
  179.             /* attempts to send data to the Macintosh IIfx when its serial driver is */
  180.             /* not yet open and it holding off data with hardware handshaking. In    */
  181.             /* such a case, data will flow in immediately when the serial driver     */
  182.             /* opens--quickly filling up the default 64-byte buffer. If the buffer   */
  183.             /* fills up before setting a larger buffer with SerSetBuf(), SerGetBuf() */
  184.             /* "sticks" and stubbornly maintains that there is nothing coming in.    */
  185.             /* At 9600 baud, it takes only a little more than four ticks to fill the */
  186.             /* input buffer. This application demonstrates the bug by virtue of the  */
  187.             /* following delay.                                                      */
  188.             
  189.             Delay(5, &finalTicks);
  190.             
  191.             /* It's always good to first set a non-default input buffer, if desired. */
  192.             /* There is no output buffering, so specify only the input driver.       */
  193.             
  194.             setBufErr = SerSetBuf(gInRefNum, gpSerBuf, kSerBufSize);
  195.             
  196.             hskFlags.fXOn = false;
  197.             hskFlags.fCTS = true;
  198.             hskFlags.xOn = 0x11;
  199.             hskFlags.xOff = 0x13;
  200.             hskFlags.errs = 0;
  201.             
  202.             if (gSysVersion >= 0x0700) {
  203.                 hskFlags.evts = 0;                /* I can use new means of break detection. */
  204.             }
  205.             else {
  206.                 hskFlags.evts = breakEvent;        /* I need the driver to post break events. */
  207.             }
  208.             
  209.             hskFlags.fInX = false;
  210.             hskFlags.fDTR = true;
  211.             
  212.             /* SerHShake() does not support full DTR/CTS hardware handshaking. You   */
  213.             /* accomplish the same thing and more with a Control call and csCode 14. */
  214.             /* You only need to specify hskFlags once, to the output driver.         */
  215.             
  216.             setHskErr = Control(gOutRefNum, kSerHShakeDTR, (Ptr) &hskFlags);
  217.  
  218.             /* Now reset both input and output drivers with the same configuration.  */
  219.             /* Only a single call to the output driver is necessary to do this.      */
  220.             /* Differing concurrent input/output baud rates are not supported.       */
  221.             
  222.             setCfgErr = SerReset(gOutRefNum, kSerConfig);
  223.             
  224.         }
  225.     }
  226.     
  227.     return takeOverPort;
  228. }
  229.  
  230.  
  231.  
  232. void CloseSERD (void)
  233. {
  234.     OSErr    killErr, closeOutErr, closeInErr;
  235.  
  236.     killErr = KillIO(gInRefNum);
  237.     closeInErr = CloseDriver(gInRefNum);
  238.  
  239.     killErr = KillIO(gOutRefNum);
  240.     closeOutErr = CloseDriver(gOutRefNum);
  241.  
  242. }
  243.  
  244.  
  245.  
  246. void CheckSerStatus (void)
  247. {
  248.     OSErr        checkStatErr, panicErr;
  249.     IOParam        altSendPB, *pAltSendPB = &altSendPB;
  250.     CntrlParam    statPB;
  251.     SerStaRec    serStat;
  252.     
  253.     statPB.ioCRefNum = gInRefNum;
  254.     statPB.csCode = kSerStatus;
  255.     checkStatErr = PBStatusImmed((ParmBlkPtr) &statPB);
  256.     serStat =  *(SerStaRec *) &statPB.csParam;
  257.     
  258.     /* I check to see if the remote system has told me to stop sending. */
  259.     
  260.     gHeldOff = (Boolean) serStat.ctsHold;
  261.     
  262.     /* Check for errors. */
  263.     
  264.     if (serStat.cumErrs & swOverrunErr)
  265.         DebugStr("\pSoftware Overrun Error");
  266.     else if (serStat.cumErrs & parityErr)
  267.         DebugStr("\pParity Error");
  268.     else if (serStat.cumErrs & hwOverrunErr)
  269.         DebugStr("\pHardware Overrun Error");
  270.     else if (serStat.cumErrs & framingErr)
  271.         DebugStr("\pFraming Error");
  272.     
  273.     /* If I have System 7.0 or better, I can check directly to see if   */
  274.     /* I've received a break. Usually I don't check for a feature this  */
  275.     /* way, but in this case I have no alternative.                     */
  276.     
  277.     if (gSysVersion >= 0x0700) {
  278.         gBreakReceived = (serStat.cumErrs & breakErr) != 0;
  279.     }
  280.     
  281.     /* All I do here is send a small "panic" packet of characters back  */
  282.     /* to the remote system when it fills _my_ buffer. I don't actually */
  283.     /* know the exact state of my buffer, but I can see if I've told    */
  284.     /* the remote system to shut up, indicating that I'm mostly full.   */
  285.     
  286.     if (checkStatErr == noErr) {
  287.         if (( (unsigned char) serStat.xOffSent & dtrNegated) != 0) {
  288.             pAltSendPB->ioCompletion = NULL;
  289.             pAltSendPB->ioRefNum = gOutRefNum;
  290.             pAltSendPB->ioBuffer = panicString;
  291.             pAltSendPB->ioReqCount = strlen(panicString);
  292.             PBWriteAsync((ParmBlkPtr) pAltSendPB);
  293.             
  294.             /* The program may hang here if the user quits the    */
  295.             /* remote application first--that could hold off our  */
  296.             /* serial output, leaving a pending asynchronous I/O  */
  297.             /* request and keeping us in an infinite loop.        */
  298.             
  299.             while (pAltSendPB->ioResult > 0) {}                /* I'll fix it later. */
  300.             /* The reason I do this instead of just calling it synchronously */
  301.             /* is so that if I do hang, I'll hang in my code for an obvious  */
  302.             /* reason instead of hanging up in the Device Manager.           */
  303.             panicErr = pAltSendPB->ioResult;
  304.         }
  305.     }
  306. }
  307.             
  308.  
  309.  
  310. void CheckSerData (long reqBytes)
  311. {
  312.     OSErr    checkBufErr, serRdErr;
  313.     long    charCount;
  314.     
  315.     // long    finalTicks;
  316.     // register long overrun;
  317.     
  318.     checkBufErr = SerGetBuf(gInRefNum, &charCount);
  319.     if (checkBufErr == noErr) {
  320.     
  321.         /* The general strategy here is this: if number of available characters */
  322.         /* meets a certain minimum threshold, then I read in everything in the  */
  323.         /* buffer. If I get delayed, I'll catch up quickly.                     */
  324.  
  325.         if (charCount != 0 && charCount >= reqBytes) {
  326.             /*
  327.             overrun = charCount;
  328.             Control(gOutRefNum, 18, nil);
  329.             Delay(300, &finalTicks);
  330.             Control(gOutRefNum, 17, nil);
  331.             SerGetBuf(gInRefNum, &charCount);
  332.             overrun = charCount - overrun;
  333.             DebugStr("\pSender Halted");
  334.             */
  335.             serRdErr = FSRead(gInRefNum, &charCount, gpBitBucket);
  336.             if (serRdErr == noErr) {
  337.                 gRcvCount++;            /* increment a counter for the input spinner */
  338.             }
  339.         }
  340.     }
  341. }
  342.  
  343.  
  344.  
  345. void DoIOStuff (void)
  346. {
  347.     DialogPtr        serMonitor;
  348.     OSErr            primeErr;
  349.     short            itemHit, itemType;
  350.     ControlHandle    sendItem, stopItem, breakItem;
  351.     Handle            spinner, flag, item;
  352.     Rect            box;
  353.     CntrlParam        breakPB;
  354.     
  355.     serMonitor = GetNewDialog(rMonitor, NULL, NULL);
  356.     SetPort((GrafPtr) serMonitor);
  357.             
  358.     while (!gAllDone) {
  359.         
  360.             CheckSerStatus();
  361.         
  362.         /* Holding down the mouse button prevents checking the input buffer     */
  363.         /* and forces the input buffer to fill up. This allows experimentation. */
  364.             
  365.             if (!Button()) {
  366.                 CheckSerData(kSerRdSize);
  367.             }
  368.     
  369.             if (gShouldSend && gReload) {
  370.                 gSendCount++;                /* increment a counter for the output spinner */
  371.                 gReload = !gReload;
  372.             }
  373.             
  374.         /* The break timer simply sets a global flag which I use to indicate when  */
  375.         /* to clear a break condition. Again, I use an immediate Control call, but */
  376.         /* primarily for consistency, and also to show off.                        */
  377.             
  378.             if (gKillBreak) {
  379.                 breakPB.ioCRefNum = gOutRefNum;
  380.                 breakPB.csCode = kSerClrBrk;
  381.                 PBControlImmed((ParmBlkPtr) &breakPB);        /* SerClrBrk(), but IMMED */
  382.                 gKillBreak = !gKillBreak;
  383.                 GetDItem(serMonitor, kBreakButton, &itemType, &(Handle) breakItem, &box);
  384.                 HiliteControl(breakItem, kCtlEnable);
  385.             }
  386.         
  387.         /* If another area of the program detects a break, I flag the occurrence here. */
  388.         
  389.             if (gBreakReceived) {
  390.                 SysBeep(1);
  391.                 SysBeep(1);
  392.                 gBreakReceived = !gBreakReceived;
  393.             }
  394.         
  395.         /* Here I update all the buttons and icons. I probably spend too much time    */
  396.         /* doing this when it isn't necessary, but that's not the point.              */
  397.         
  398.         /* I animate the beach balls by changing the resource IDs of the ICONs in the */
  399.         /* DITL and invalidating their enclosing rectangles. I also hilight controls  */
  400.         /* appropriately and display annunciators when necessary.                     */
  401.             
  402.             GetDItem(serMonitor, kSendButton, &itemType, &(Handle) sendItem, &box);
  403.             GetDItem(serMonitor, kStopButton, &itemType, &(Handle) stopItem, &box);
  404.             if (gShouldSend) {
  405.                 HiliteControl(sendItem, kCtlDisable);
  406.                 HiliteControl(stopItem, kCtlEnable);
  407.             }
  408.             else {
  409.                 HiliteControl(sendItem, kCtlEnable);
  410.                 HiliteControl(stopItem, kCtlDisable);
  411.             }
  412.             
  413.             spinner = Get1Resource('ICON', rSpinnerIcon + gSendCount % 8);
  414.             GetDItem(serMonitor, kSendSpinner, &itemType, &item, &box);
  415.             SetDItem(serMonitor, kSendSpinner, iconItem, spinner, &box);
  416.             InvalRect(&box);
  417.             
  418.             spinner = Get1Resource('ICON', rSpinnerIcon + gRcvCount % 8);
  419.             GetDItem(serMonitor, kRcvSpinner, &itemType, &item, &box);
  420.             SetDItem(serMonitor, kRcvSpinner, iconItem, spinner, &box);
  421.             InvalRect(&box);
  422.             
  423.             if (gShouldSend && gHeldOff) {
  424.                 flag = Get1Resource('ICON', rHeldIcon);
  425.             }
  426.             else {
  427.                 flag = Get1Resource('ICON', rNotHeldIcon);
  428.             }
  429.             GetDItem(serMonitor, kHoldFlag, &itemType, &item, &box);
  430.             SetDItem(serMonitor, kHoldFlag, iconItem, flag, &box);
  431.             InvalRect(&box);
  432.                 
  433.         /* In lieu of an event loop, I just use a modal dialog with a relatively  */
  434.         /* simple (but unusual) filterProc. This is not a good example of how to  */
  435.         /* write an app. Modal dialogs are evil and to be avoided if possible.    */
  436.         /* Nonetheless, the filterProc is an interesting example unto itself....  */
  437.     
  438.             ModalDialog(NullGrabber, &itemHit);
  439.             switch (itemHit) {
  440.                 
  441.                 case kStopButton:
  442.                     if (gShouldSend) {
  443.                         gShouldSend = !gShouldSend;
  444.                     }
  445.                     break;
  446.                 
  447.                 case kSendButton:
  448.                     if (!gShouldSend) {
  449.                         gShouldSend = !gShouldSend;
  450.                         primeErr = PrimeSend();
  451.                     }
  452.                     break;
  453.                 
  454.                 case kBreakButton:
  455.                 
  456.                 /* In another possible Mac IIfx IOP bug, SerSetBrk() called synchronously  */
  457.                 /* appears to hang the machine if an async write is pending. Since that is */
  458.                 /* often the case (at least in this application) I work around the problem */
  459.                 /* by making the Control call immediate--this prevents the hang, but also  */
  460.                 /* raises another interesting issue about when break is actually asserted. */
  461.                 
  462.                     breakPB.ioCRefNum = gOutRefNum;
  463.                     breakPB.csCode = kSerSetBrk;
  464.                     PBControlImmed((ParmBlkPtr) &breakPB);        /* SerSetBrk(), but IMMED */
  465.                     
  466.                 /* With break asserted, I prime a Time Manager task to flag the end of    */
  467.                 /* the break, and disable the Break button so that it cannot be selected  */
  468.                 /* again until break is negated.                                          */
  469.                 
  470.                     PrimeTime((QElemPtr) &gUnBreakTask, kBreakLength);
  471.                     GetDItem(serMonitor, kBreakButton, &itemType, &(Handle) breakItem, &box);
  472.                     HiliteControl(breakItem, kCtlDisable);
  473.                     break;
  474.                 
  475.                 case kQuitButton:
  476.                     gAllDone = true;
  477.                     break;
  478.                 
  479.             }
  480.     }
  481.     
  482.     DisposDialog(serMonitor);
  483.     
  484. }
  485.  
  486.  
  487.  
  488. pascal Boolean NullGrabber (DialogPtr, EventRecord *evt, short *itemHit)
  489. {
  490.     EventRecord    driverEvent;
  491.     
  492.     /* Without this filterProc, none of the animation works: */
  493.     
  494.     /* In order to keep things rolling along even when there are no events    */
  495.     /* such as mouse clicks or keystrokes, I have to return true in response  */
  496.     /* to null events. This is unusual, but otherwise ModalDialog() "handles" */
  497.     /* null events by eating them and waiting for something better. This is   */
  498.     /* bad if I need to turn the spinner or clear a break condition.          */
  499.     
  500.     /* Also, I check for driver events here in order to detect breaks. */
  501.     
  502.     if (GetNextEvent(driverMask, &driverEvent)) {
  503.         if ((*(SERDEventMessage *) &driverEvent.message).drvrPosting == gInRefNum) {
  504.             gBreakReceived = ((*(SERDEventMessage *) &driverEvent.message).readR0 & breakR0) != 0;
  505.         }
  506.     }
  507.     
  508.     if (evt->what == nullEvent) {
  509.         *itemHit = 0;
  510.         return true;
  511.     }
  512.     
  513.     else {
  514.         return false;
  515.     }
  516.     
  517. }
  518.  
  519.  
  520.  
  521. pascal void SendCompRout (void)
  522. {
  523.     long    oldA5;
  524.     
  525.     oldA5 = SetA5(((ExtIOParam *) GetParmBlkPtr())->appA5);        /* retrieve A5 */
  526.     
  527.     if (gShouldSend && !gAllDone) {
  528.         gReload = true;
  529.         gpSendPB->ioCompletion = (ProcPtr) SendCompRout;
  530.         PBWriteAsync((ParmBlkPtr) gpSendPB);            /* this is the self-sustaining part */        
  531.     }
  532.     
  533.     SetA5(oldA5);
  534.     
  535. }
  536.  
  537.  
  538.  
  539. OSErr PrimeSend (void)
  540. {
  541.     gpSendPB->ioCompletion = (ProcPtr) SendCompRout;
  542.     gpSendPB->ioRefNum = gOutRefNum;
  543.     gpSendPB->ioBuffer = gpOutputData;
  544.     gpSendPB->ioReqCount = gOutputDataSize;
  545.     ((ExtIOParam *) gpSendPB)->appA5 = SetCurrentA5();    /* completion routine needs A5 */
  546.     gReload = true;
  547.     
  548.     return PBWriteAsync((ParmBlkPtr) gpSendPB);            /* asynchronous self-sustaining sends */
  549.  
  550. }
  551.  
  552.  
  553.  
  554. pascal void FlagBreakTimeout (void)
  555. {
  556.     long    oldA5;
  557.     
  558.     oldA5 = SetA5(((ExtTMTask *) GetTMTaskPtr())->appA5);        /* retrieve A5 */
  559.     
  560.     gKillBreak = true;
  561.     
  562.     SetA5(oldA5);
  563.     
  564. }
  565.  
  566.  
  567.  
  568. OSErr AssertDrvrOpen (Str255 name, short *refNum)
  569. {
  570.     DCtlHandle    *pUTEntry;
  571.     Ptr            pDrvr;
  572.     OSErr        result = notOpenErr;        /* assume not open */
  573.     short        unitNo;
  574.     char        *aDrvrName;
  575.     
  576.     /* The point here is to determine whether a driver is open, given its name.  */
  577.     /* This allows one to check a driver to see if it's open without hard coding */
  578.     /* its reference number. (Normally, the way to get the refNum is to open     */
  579.     /* the driver--but that defeats the whole purpose!)                          */
  580.     /* This is an extension of the code discussed in Tech Note #71.              */
  581.     
  582.     *refNum = 0;
  583.     pUTEntry = *(DCtlHandle **) UTableBase;
  584.     for (unitNo = 0; unitNo < *(short *) UnitNtryCnt; unitNo++, pUTEntry++) {
  585.         if (*pUTEntry != nil && **pUTEntry != nil) {
  586.             if (((***pUTEntry).dCtlFlags & 1 << dRAMBased) != 0)
  587.                 pDrvr = *(Handle) (***pUTEntry).dCtlDriver;
  588.             else
  589.                 pDrvr = (***pUTEntry).dCtlDriver;
  590.             
  591.             if (pDrvr != nil) {
  592.                 aDrvrName = pDrvr + drvrName;
  593.                 if (memcmp(aDrvrName, name, 1 + name[0]) == 0) {
  594.                     /* We found the one we're after. */
  595.                     *refNum = ~unitNo;
  596.                     if (((***pUTEntry).dCtlFlags & 1 << dOpened) != 0)
  597.                         result = noErr;
  598.                     break;
  599.                 }
  600.             }
  601.         }
  602.     }    
  603.     
  604.     return result;
  605. }
  606.  
  607.  
  608.  
  609. short Initialize (void)
  610. {
  611.     long    result;
  612.     OSErr    gestErr;
  613.     
  614.     gestErr = Gestalt(gestaltSystemVersion, &result);
  615.     gSysVersion = result;
  616.     
  617.     ghSendPB = (IOParam **) NewHandle( sizeof(ExtIOParam));
  618.     if (ghSendPB != NULL) {
  619.         MoveHHi((Handle) ghSendPB);
  620.         HLock((Handle) ghSendPB);
  621.         gpSendPB = *ghSendPB;
  622.     }
  623.     else {
  624.         return MemError();
  625.     }
  626.  
  627.     ghSerBuf = NewHandle(kSerBufSize);
  628.     if (ghSerBuf != NULL) {
  629.         MoveHHi((Handle) ghSerBuf);
  630.         HLock((Handle) ghSerBuf);
  631.         gpSerBuf = *ghSerBuf;
  632.     }
  633.     else {
  634.         DisposHandle((Handle) ghSendPB);
  635.         return MemError();
  636.     }
  637.     
  638.     ghBitBucket = NewHandle(kSerBufSize);
  639.     if (ghBitBucket != NULL) {
  640.         MoveHHi(ghBitBucket);
  641.         HLock(ghBitBucket);
  642.         gpBitBucket = *ghBitBucket;
  643.     }
  644.     else {
  645.         DisposHandle((Handle) ghSendPB);
  646.         DisposHandle((Handle) ghSerBuf);
  647.         return MemError();
  648.     }
  649.     
  650.     ghOutputData = Get1Resource('sDAT', rSerDataRsrc);
  651.     if (ghOutputData != NULL) {
  652.         MoveHHi(ghOutputData);
  653.         HLock(ghOutputData);
  654.         gpOutputData = *ghOutputData;
  655.         gOutputDataSize = SizeResource(ghOutputData);
  656.     }
  657.     else {
  658.         DisposHandle((Handle) ghSendPB);
  659.         DisposHandle((Handle) ghSerBuf);
  660.         DisposHandle(ghBitBucket);
  661.         return ResError();
  662.     }
  663.     
  664.     /* I use a Time Manager task to time breaks. This is where I install it. */
  665.     
  666.     gUnBreakTask.fTMTask.tmAddr = FlagBreakTimeout;
  667.     gUnBreakTask.appA5 = SetCurrentA5();
  668.     InsTime((QElemPtr) &gUnBreakTask);
  669.     
  670.     return noErr;
  671. }
  672.  
  673.  
  674.  
  675. void CleanUp (void)
  676. {
  677.     /* I remove the Time Manager task before exiting. */
  678.     
  679.     RmvTime((QElemPtr) &gUnBreakTask);
  680.  
  681.     DisposHandle((Handle) ghSendPB);
  682.     DisposHandle(ghSerBuf);
  683.     DisposHandle(ghBitBucket);
  684. }
  685.